class AJAXP {
    constructor() {
        throw new Error("工具类，无序实例化");
    }

    static GET = (pathname, args_arr_ky = [], callback) => {
        // args_arr_ky --> ["key=value","key1=value1"]
        let action = pathname + "?" + args_arr_ky.join("&");
        let xhr = new XMLHttpRequest();
        xhr.open("GET", action);
        xhr.send();
        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4 && xhr.status === 200) {
                if (xhr.responseText) {
                    callback(xhr.responseText);
                }
            }
        };
    }
    static POST = (pathname, args_obj = {}, headers = {}, callback, ) => {
        // args_obj --> { key="value", key1="value1" }
        let xhr = new XMLHttpRequest();
        xhr.open("POST", pathname);
        if (Object.keys(headers).length === 0)
            headers = Object.assign(headers, {
                "key": "content-type",
                "value": "application/json",
                // "value": "application/x-www-form-urlencoded"
            });
        xhr.setRequestHeader(headers["key"], headers["value"]);
        xhr.send(JSON.stringify(args_obj));
        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4 && xhr.status === 200) {
                callback(xhr.responseText);
            }
        };
    }
    static JSONP = (serverRootPath, args_arr_ky, call_fun) => {
        // serverRoot += serverRoot.substr(-1) === "/" ? "" : "/";
        let temp = [];
        if (!Array.isArray(args_arr_ky)) {
            let keys = Object.keys(args_arr_ky);
            keys.forEach(key => {
                let kv = `${key}=${args_arr_ky[key]}`;
                temp.push(kv);
            });
            args_arr_ky = temp;
        }

        args_arr_ky.push(`callback=${call_fun}`);
        let action = serverRootPath + "?" + args_arr_ky.join("&");
        let scriptTag = document.createElement("script");
        scriptTag.src = action;
        document.body.appendChild(scriptTag);
    }
}


// AMD 导出模块
// define(
//     () => {
//         return AJAXP;
//     }
// );